package com.huat.sportmeetingmanagement.controller;

import com.github.pagehelper.PageInfo;
import com.huat.sportmeetingmanagement.entity.GeneralVO;
import com.huat.sportmeetingmanagement.entity.Project;
import com.huat.sportmeetingmanagement.entity.Record;
import com.huat.sportmeetingmanagement.entity.enumEntity.ErrorListEnum;
import com.huat.sportmeetingmanagement.service.ProjectService;
import com.huat.sportmeetingmanagement.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("project")
public class ProjectController {
    @Autowired
    ProjectService projectService;
    @Autowired
    RecordService recordService;
    @GetMapping("/getProjectById")
    public GeneralVO getProjectById(Integer id){
        Project Project = projectService.getProjectById(id);
        return new GeneralVO(ErrorListEnum.E200,Project);

    }
    @GetMapping("/queryAllProjectsByStatus")
    public GeneralVO queryAllProjectsByStatus(Integer status){
        List<Project> Projects = projectService.queryAllProjectsByStatus(status);
        return new GeneralVO(ErrorListEnum.E200,Projects);
    }
    @PostMapping("/getProjects")
    public GeneralVO getProjects(@RequestBody Project project){
        List<Project> Projects = projectService.getProjects(project);
        PageInfo<Project> ProjectPageInfo = new PageInfo<>(Projects);
        return new GeneralVO(ErrorListEnum.E200,ProjectPageInfo);
    }
    @PostMapping("/updateProject")
    public GeneralVO updateProject(@RequestBody Project project){
        Integer result = 0;
        try{
            result = projectService.updateProject(project);}
        catch (Exception ex){
            return new GeneralVO(ErrorListEnum.E60001,"该比赛项目已存在");
        }
        return new GeneralVO(ErrorListEnum.E200,result);
    }
    @PostMapping("/addProject")
    public GeneralVO addProject(@RequestBody Project project){
        Integer result = 0;
        try{
            result = projectService.addProject(project);}
        catch (Exception ex){
            return new GeneralVO(ErrorListEnum.E60001,"该比赛项目已存在");
        }
        return new GeneralVO(ErrorListEnum.E200,result);
    }

    @DeleteMapping("/{ids}")
    public GeneralVO deleteProjects(@PathVariable String[] ids){
        Integer result = 0;
        for (String no :ids) {
            Record record = new Record();
            record.setProjectNo(no);
            if (recordService.getRecordById(record)!=null)
                return new GeneralVO(ErrorListEnum.E20004,"有数据与该项目绑定");
        }
        result = projectService.deleteProjects(ids);
        return new GeneralVO(ErrorListEnum.E200,result);
    }

}